(() => {
    let isMay = false; //是否可以进行注册,默认不可以
    const signin = document.querySelector('.signin');

    userName.addEventListener('blur', async(e) => {
        const loginId = e.target.value.trim();
        const judge = await (async() => {
            try {
                const jud = await fetch(
                    `https://study.duyiedu.com/api/user/exists/?loginId=${loginId}`
                );
                return await jud.json();
            } catch (err) {
                console.log(err);
            }
        })();
        console.log(judge);
        if (judge.data === null) {
            window.alert(judge.msg);
        } else if (judge.data === true) {
            window.alert('该账号已存在');
            userName.value = '';
        }
    });
    //表单提交事件
    signin.addEventListener('click', async(e) => {
        //阻止form表单默认提交刷新页面事件
        e.preventDefault();
        /**判断当前是否满足提交要求
         * 1. 需要判断所有列表都有值。
         * 2. 判断两次密码是否一样
         */
        if (!(
                isUserName() &&
                isUserNickname() &&
                isUserPassword() &&
                isUserConfirmPassword()
            )) {
            return;
        }
        //满足条件执行注册
        const enroll = await (async() => {
            try {
                const user = await fetch('https://study.duyiedu.com/api/user/reg', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        loginId: userName.value.trim(),
                        nickname: userNickname.value.trim(),
                        loginPwd: userPassword.value.trim(),
                    }),
                });
                const token = user.headers.get('Authorization');

                if (token) {
                    sessionStorage.setItem('token', token);
                }
                return await user.json();
            } catch (err) {
                console.log(err);
            }
        })();

        if (enroll.code === 0) {
            window.open('./index.html', '_self');
        }
    });
    isUserName = () => {
        if (!userName.value.trim()) {
            window.alert('账号不能为空');
            return false;
        } else {
            return true;
        }
    };
    isUserNickname = () => {
        if (!userNickname.value.trim()) {
            window.alert('用户昵称不能为空');
            return false;
        } else {
            return true;
        }
    };
    isUserPassword = () => {
        if (!userPassword.value.trim()) {
            window.alert('密码不能为空');
            return false;
        } else {
            return true;
        }
    };
    isUserConfirmPassword = () => {
        if (!userConfirmPassword.value.trim()) {
            window.alert('验证密码不能为空');
            return false;
        } else if (userPassword.value.trim() !== userConfirmPassword.value.trim()) {
            window.alert('两次密码输入不一致');
            return false;
        } else {
            return true;
        }
    };
})();